import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);

/* Layout */
import Layout from "@/layout";
import { clearCacheRouter } from "@/utils/menu";

let flush = true;

export const constantRoutes = [
  {
    id: 1001,
    path: "/login",
    component: () => import("@/views/login/index"),
    hidden: true,
  },

  {
    path: "/404",
    component: () => import("@/views/404"),
    hidden: true,
  },
  {
    path: "/",
    component: Layout,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        name: "Dashboard",
        component: () => import("@/views/home/index"),
        meta: { title: "首页", icon: "dashboard" },
      },
      {
        path: "/releaseNews",
        component: () => import("@/views/news/NewsReleaseNews.vue"),
        hidden: true,
      },
      {
        path: "/newsDetail",
        component: () => import("@/views/news/NewsDetailNews.vue"),
        hidden: true,
      },
      {
        path: "/alterNews",
        component: () => import("@/views/news/NewsAlterNews.vue"),
        hidden: true,
      },
      {
        path: "/publishAd",
        hidden: true, // 导航隐藏
        component: () => import("@/views/ad/cpns/AdAdvertise"),
      },
      {
        path: "/adDetailById/:advertId/:type",
        hidden: true,
        component: () => import("@/views/ad/cpns/AdDetail.vue"),
        props: true,
      },
      {
        path: "/adTime",
        hidden: true,
        component: () => import("@/views/ad/cpns/AdTime.vue"),
      },
    ],
  },

  // 404 page must be placed at the end !!!
  { path: "*", redirect: "/", hidden: true },
];

const createRouter = () => {
  clearCacheRouter();
  return new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes,
  });
};

const router = createRouter();

export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export function addRouters(dynamicRoutes) {
  constantRoutes.push(...dynamicRoutes);
  resetRouter();
}

export default router;
